package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Fens;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Rankdesc;
import com.qianniu.napi.admin.entity.penum.RankTypeEnum;
import com.qianniu.napi.admin.service.IFensService;
import com.qianniu.napi.admin.service.IRankdescService;
import com.qianniu.napi.admin.service.IUserService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户粉丝表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-07
 */
@RestController
public class FensController {

    private static final Logger logger = LoggerFactory.getLogger(FensController.class);

    @Autowired
    private IFensService fensService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRankdescService rankdescService;

    @RequestMapping("/api/fens/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "init", required = false) String init,
                      @RequestParam(value = "test", required = false) Integer test,
                      @RequestParam(value = "level_type", required = false) int level_type,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;

        Long uid = ObjectUtil.getUid();
        if (uid == null) return json;

        Map<String, Object> map = new HashMap<String, Object>();
        Page page = new Page(pageIndex, pageSize);
        Wrapper wrapper = new EntityWrapper<Fens>()
                .eq("puid", uid)
                .eq("level_type", level_type)
                .eq("state", 1);
        Page<Fens> fs1 = fensService.selectPage(page, wrapper);

        map.put("list", fs1.getRecords());
        map.put("total", fs1.getTotal());

        if (!Strings.isNullOrEmpty(init)) {
            int total2 = fensService.selectCount(new EntityWrapper<Fens>()
                    .eq("puid", uid)
                    .eq("level_type", 2)
                    .eq("state", 1));
            map.put("total2", total2);
        }
        json.setInfo(map);
        return json;
    }

    @RequestMapping("/api/fens/add")
    @ResponseBody
    public JsonDO fensAdd(@Valid Fens fens,
                          BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        fens.setCreatetime(new Date());
        fens.insert();
        return json;
    }

    @RequestMapping("/api/fens/modify")
    @ResponseBody
    public JsonDO fensUpdate(HttpServletRequest req,
                             Fens fens) {
        JsonDO json = new JsonDO();
        fens.setModifytime(new Date());
        fens.updateById();
        return json;
    }

    @RequestMapping("/api/fens/detail")
    @ResponseBody
    public JsonDO getFensDetailById(@RequestParam("token") String token,
                                    @RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        Fens fens = fensService.selectById(id);
        json.setData(fens);
        return json;
    }

    @RequestMapping("/api/ranking/list")
    @ResponseBody
    public Jinfo getRankingList(@RequestParam("timeType") int timeType,
                                @RequestParam("rankType") int rankType,
                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        Jinfo json = new Jinfo();
        Map<String, Object> map = new HashMap<String, Object>();
        pageIndex = pageIndex == null ? 1 : pageIndex;
        pageSize = pageSize == null ? 10 : pageSize;
        map.put("mpid", ObjectUtil.getMpid());

//        Rankdesc rankDesc = rankdescService.selectOne(new EntityWrapper<Rankdesc>().setSqlSelect("id,avatarpic,nickname,count(1) total")
//                .groupBy("puid")
//                .eq("mid",mid));
        Rankdesc rankDesc = rankdescService.selectOne(new EntityWrapper<Rankdesc>().eq("mid", ObjectUtil.getMid()));

        map.put("timeType", timeType);
        map.put("skip", (pageIndex - 1) * pageSize);
        map.put("size", pageSize);

        List<Fens> fens = null;
        if (rankType == RankTypeEnum.fens.getVal()) {
//            int cout  = fensService.getRankingCout(map);
            fens = fensService.getRankingList(map);
        }
        map.clear();
        map.put("list", fens);
        if (rankDesc != null) map.put("rankDesc", rankDesc.getDesc());
        json.setInfo(map);
        return json;
    }

}
